package com.woshua.aopmodel.aspect;

import com.woshua.aopmodel.utils.SumUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

/*
 * 用户行为切面统计
 * */
@Aspect
public class BehaviorAspect {

    /*
    * 带有BehaviorTrace的方法构成的切面
    * */
    @Pointcut("execution(@com.woshua.aopmodel.aspect.BehaviorTrace * *(..))")
    public void annoBehaviorTrace() { }

    @Around("annoBehaviorTrace()")
    public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable
    {

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

        //类名
        String className=methodSignature.getDeclaringType().getSimpleName();
        String thName=methodSignature.getName();

        //功能名称
        BehaviorTrace behaviorTrace = methodSignature.getMethod().getAnnotation(BehaviorTrace.class);
        String fun = behaviorTrace.value();
        long begin = SumUtils.begin();
        Object result = joinPoint.proceed();
        SumUtils.end(fun,className,thName,begin);
        return result;
    }
}
